package com.itheima.mall.dubbo.api.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.commons.page.PageResult;
import com.itheima.mall.dubbo.mapper.ums.UmsAdminMapper;
import com.itheima.mall.entity.domain.query.ums.AdminQuery;
import com.itheima.mall.entity.domain.ums.Admin;
import com.itheima.mall.entity.domain.ums.AdminRoleRelation;
import com.itheima.mall.entity.domain.ums.Menu;
import com.itheima.mall.entity.domain.ums.Role;
import com.itheima.mall.entity.domain.vo.ums.AdminVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@DubboService
@Transactional
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, Admin> implements UmsAdminService {

    @Autowired
    private AdminRoleRelationServiceImpl adminRoleRelationService;

    @Autowired
    private RoleImplService roleImplService;

    @Autowired
    private UmsAdminMapper adminMapper;

    @Override
    public void delete(Integer id) {
        Admin admin = new Admin().setId(id);
        this.removeById(admin);
        adminRoleRelationService.remove(new LambdaQueryWrapper<AdminRoleRelation>().eq(Objects.nonNull(id), AdminRoleRelation::getAdminId, id));
    }

    @Override
    public AdminVo info(String username) {
        List<Role> role = adminMapper.getRoles(username);
        List<Integer> roleIds = role.stream().map(item ->
                item.getId()
        ).collect(Collectors.toList());

        List<String> roles = role.stream().map(item ->
                item.getName()
        ).collect(Collectors.toList());
        List<Menu> menus = adminMapper.getMenus(roleIds);
        AdminVo adminVo = new AdminVo();
        adminVo.setUsername(username);
        adminVo.setRoles(roles);
        adminVo.setMenus(menus);
        return adminVo;
    }

    @Override
    public PageResult pageInfo(String keyword, Integer pageNum, Integer pageSize) {
        Page<Admin> adminPage = new Page<>(pageNum, pageSize);
        Page<Admin> page = this.page(adminPage, new LambdaQueryWrapper<Admin>().like(Objects.nonNull(keyword), Admin::getUsername, keyword));
        PageResult init = PageResult.init(page.getRecords(), page.getCurrent(), page.getSize(), page.getTotal());
        return init;
    }

    @Override
    public void roleUpdate(AdminQuery adminQuery) {
        adminRoleRelationService.remove(new LambdaQueryWrapper<AdminRoleRelation>().eq(Objects.nonNull(adminQuery.getAdminId()),AdminRoleRelation::getAdminId,adminQuery.getAdminId()));
        List<AdminRoleRelation> collect = adminQuery.getRoleIds().stream().map(item -> {
            AdminRoleRelation adminRoleRelation = new AdminRoleRelation();
            adminRoleRelation.setAdminId(Long.valueOf(adminQuery.getAdminId()));
            adminRoleRelation.setRoleId(Long.valueOf(item));
            return adminRoleRelation;
        }).collect(Collectors.toList());
        adminRoleRelationService.saveBatch(collect);
    }

    @Override
    public List<Role> role(Integer adminId) {
        List<Role> role = adminMapper.role(adminId);
        return role;
    }

    @Override
    public void adminUpdate(Admin admin) {
        this.updateById(admin);
    }

    @Override
    public void updatePassword(Map<String, Object> map) {
        Admin admin = new Admin();
        admin.setUsername(String.valueOf(map.get("username")));
        admin.setPassword(DigestUtils.md5Hex(String.valueOf(map.get("newPassword"))));
        this.updateById(admin);
    }

    @Override
    public void updateStatus(Integer id, Admin admin) {
        admin.setId(id);
        this.updateById(admin);
    }

    @Override
    public Admin admin(Integer id) {
        Admin admin = this.getById(id);
        return admin;
    }
}
